package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.ManagementType;
import cn.fintecher.pangolin.common.model.DepartmentModel;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.business.model.request.LeaveCaseRequest;
import cn.fintecher.pangolin.service.business.model.response.LeaveCaseResponse;
import cn.fintecher.pangolin.service.business.repository.jparepository.CaseInfoRepository;
import cn.fintecher.pangolin.service.business.service.BaseService;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author wangxiangdong
 * @Description 留案案件管理
 * @Date 2019/9/17 14:50
 **/
@RestController
@RequestMapping("/api/leaveCaseController")
@Api(value = "留案案件管理", description = "留案案件管理")
public class LeaveCaseController {

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private BaseService baseService;

    @GetMapping("/queryLeaveCases")
    @ApiOperation(value = "留案案件查询", notes = "留案案件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<LeaveCaseResponse>> queryLeaveCases(Pageable pageable, LeaveCaseRequest leaveCaseRequest, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        DepartmentModel departmentModel = baseService.getDepartmentByToken(token);
        BooleanBuilder booleanBuilder = leaveCaseRequest.generateBooleanBuilder();
        if (Objects.equals(userModel.getIsManage().name(), ManagementType.YES.name())) {
            booleanBuilder.and(QCaseInfo.caseInfo.currentCollector.department.departCode.like(StringUtils.join(departmentModel.getDepartCode(), "%")));
        } else {
            booleanBuilder.and(QCaseInfo.caseInfo.currentCollector.id.eq(userModel.getId()));
        }
        Sort sort = new Sort(Sort.Direction.ASC, "delegationDate")
                .and(new Sort(Sort.Direction.ASC, "followTime"))
                .and(new Sort(Sort.Direction.DESC, "overdueAmtTotal"));
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<CaseInfo> caseInfoPage = caseInfoRepository.findAll(booleanBuilder, pageable);
        Page<LeaveCaseResponse> distributedCaseResponsePage = caseInfoPage.map(x -> modelMapper.map(x, LeaveCaseResponse.class));
        return new ResponseEntity<>(distributedCaseResponsePage, HttpStatus.OK);
    }
}
